// ignore_for_file: prefer_const_constructors


import 'package:favourcate/core/extension/int_fit_ext.dart';
import 'package:favourcate/core/model/meal_model.dart';
import 'package:favourcate/core/viewmodel/favor_view_model.dart';
import 'package:favourcate/ui/pages/detail/detail.dart';
import 'package:favourcate/ui/widgets/operation_item.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class GLMealItem extends StatelessWidget {

  final GLMealModel mealItem;

  const GLMealItem(this.mealItem, { Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Card(
        margin: EdgeInsets.all(10.px),
        // 阴影
        elevation: 5,
        // 圆角
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12.px)
        ),
        child: Column(
          children: [
            buildBaseInfo(context),
            buildOperationInfo(context)
          ],
        ),
      ),
      onTap: () {
        Navigator.of(context).pushNamed(GLDetailScreen.routeName, arguments: mealItem);
      },
    );
  }

  Widget buildBaseInfo(context) {
    return Stack(
      children: [
        ClipRRect(
          child: Image.network(mealItem.imageUrl, width: double.infinity, height: 250.px, fit: BoxFit.cover,),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(12.px),
            topRight: Radius.circular(12.px)
          ),
        ),
        Positioned(
          right: 10.px,
          bottom: 10.px,
          child: Container(
            width: 300.px,
            decoration: BoxDecoration(
              color: Colors.black54,
              borderRadius: BorderRadius.circular(6.px)
            ),
            padding: EdgeInsets.symmetric(horizontal: 10.px, vertical: 5.px),
            child: Text(
              mealItem.title, 
              style: Theme.of(context).textTheme.headline3!.copyWith(
                color: Colors.white
              ),
            ),
          ),
        )
      ],
    );
  }
   Widget buildOperationInfo(context) {
    return  Padding(
      padding: EdgeInsets.all(16.px),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          GLOperationItem(Icon(Icons.schedule), '${mealItem.duration}min'),
          // mealItem.complexStr ?? ''
          GLOperationItem(Icon(Icons.restaurant), mealItem.complexStr!),
          buildFavorItem(context)
        ],
      ),
    );
  }

  Widget buildFavorItem(ctx) {
    return Consumer<GLFavorViewModel>(
      builder: (ctx, favorStore, child) {
        // 1. 判断是否收藏
        final isFavor = favorStore.isFavor(mealItem);
        final iconData = isFavor ? Icons.favorite : Icons.favorite_border;
        final favorColor = isFavor ? Colors.red : Colors.black;
        final favorText = isFavor ? '已收藏' : '未收藏';
        return GestureDetector(
          child: GLOperationItem(
            Icon(iconData, color: favorColor,),
            favorText,
            favorColor: favorColor
          ),
          onTap: () {
            favorStore.handleMeal(mealItem);
          },
        );
      },
    );
  }
}